package com.moli.iov.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moli.iov.base.mapper.VehicleUnauthMapper;
import com.moli.iov.base.model.dto.req.VehicleUnauthReqDto;
import com.moli.iov.base.model.po.VehicleUnauth;
import com.moli.iov.base.service.IVehicleUnauthService;
import com.moli.iov.model.dto.req.PageConvert;
import com.moli.iov.model.dto.req.PageExtend;
import com.moli.iov.util.StringUtil;
import java.util.Date;
import java.util.List;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 未授权登录管理
 * </p>
 *
 * @author zql
 * @since 2020/2/20 11:58
 */
@Service
public class VehicleUnauthServiceImpl extends ServiceImpl<VehicleUnauthMapper, VehicleUnauth> implements IVehicleUnauthService {

  /**
   * 增加异常信息
   * @param vehicleUnauth
   * @return
   */
  @Override
  public boolean saveVehicleUnauth(VehicleUnauth vehicleUnauth) {
    return this.save(vehicleUnauth);
  }

  /**
   * 分页查询 未授权登录
   * @param vehicleUnauthReqDto
   * @return
   */
  @Override
  public PageExtend<VehicleUnauth> listPageVehicleUnauth(
      VehicleUnauthReqDto vehicleUnauthReqDto) {

    IPage<VehicleUnauth> page = this.page(new Page<VehicleUnauth>(vehicleUnauthReqDto.getPageNum(),
            vehicleUnauthReqDto.getPageSize()),
        assemblyConditions(vehicleUnauthReqDto));

    return PageConvert.convert(page);
  }

  /**
   * 处理
   * @param vehicleUnauthReqDto
   * @return
   */
  @Override
  public boolean updateVehicleUnauthOfProcess(VehicleUnauthReqDto vehicleUnauthReqDto) {

    VehicleUnauth vehicleUnauth = new VehicleUnauth();
    vehicleUnauth.setUnauthId(vehicleUnauthReqDto.getUnauthId());
    vehicleUnauth.setStatus(vehicleUnauthReqDto.getStatus());
    vehicleUnauth.setResult(vehicleUnauthReqDto.getResult());
    vehicleUnauth.setLoginAuth(vehicleUnauthReqDto.getLoginAuth());
    vehicleUnauth.setProcessTime(new Date());

    return this.updateById(vehicleUnauth);
  }

  /**
   * 根据条件查询未授权登录记录
   * @param vehicleUnauthReqDto
   * @return
   */
  @Override
  public List<VehicleUnauth> listVehicleUnauth(VehicleUnauthReqDto vehicleUnauthReqDto) {
    return this.list(assemblyConditions(vehicleUnauthReqDto));
  }

  /**
   * 组装查询条件
   * @param vehicleUnauthReqDto
   * @return
   */
  private QueryWrapper<VehicleUnauth> assemblyConditions(VehicleUnauthReqDto vehicleUnauthReqDto){
    QueryWrapper<VehicleUnauth> queryWrapper = new QueryWrapper<>();
    queryWrapper.orderByDesc("create_time");

    if(vehicleUnauthReqDto == null){
      return queryWrapper;
    }

    if(StringUtil.isNotBlank(vehicleUnauthReqDto.getVin())){
      queryWrapper.eq("vin", vehicleUnauthReqDto.getVin());
    }

    if(vehicleUnauthReqDto.getStatus() != null){
      queryWrapper.eq("status", vehicleUnauthReqDto.getStatus());
    }

    if(vehicleUnauthReqDto.getResult() != null){
      queryWrapper.eq("result", vehicleUnauthReqDto.getResult());
    }

    if(vehicleUnauthReqDto.getBeginTime() != null &&
        vehicleUnauthReqDto.getEndTime() != null){
      queryWrapper.and( i -> i.ge("create_time", vehicleUnauthReqDto.getBeginTime())
          .le("create_time", vehicleUnauthReqDto.getEndTime()));
    }else if(vehicleUnauthReqDto.getBeginTime() != null){
      queryWrapper.ge("create_time", vehicleUnauthReqDto.getBeginTime());
    }else if(vehicleUnauthReqDto.getEndTime() != null){
      queryWrapper.le("create_time", vehicleUnauthReqDto.getEndTime());
    }

    if(vehicleUnauthReqDto.getSysCompanyId() != null){
      queryWrapper.eq("company_id", vehicleUnauthReqDto.getSysCompanyId());
    }

    return queryWrapper;
  }
}
